<?php

class NodeAction extends PublicAction {

    protected $db,$groups;

    function _initialize()
    {
        parent::_initialize();
        $this->db = D('Node');

        $this->groups[0] = array('id'=>0,'name'=>'全局设置');

        foreach($this->menudata as $key=>$r){
            if($r['parentid']==0){
                $this->groups[$r['id']] = $r;
            }
        }

        $this->assign('groups', $this->groups);
    }


    function index()
    {

        $data[]=array('name'=>'index','title'=>'列表','pid'=>90,'status'=>1,'listorder'=>0,'level'=>3,'groupid'=>3);
        $data[]=array('name'=>'add','title'=>'添加','pid'=>90,'status'=>1,'listorder'=>0,'level'=>3,'groupid'=>3);
        $data[]=array('name'=>'edit','title'=>'修改','pid'=>90,'status'=>1,'listorder'=>0,'level'=>3,'groupid'=>3);
        $data[]=array('name'=>'insert','title'=>'插入','pid'=>90,'status'=>1,'listorder'=>0,'level'=>3,'groupid'=>3);
        $data[]=array('name'=>'update','title'=>'更新','pid'=>90,'status'=>1,'listorder'=>0,'level'=>3,'groupid'=>3);
        $data[]=array('name'=>'delete','title'=>'删除','pid'=>90,'status'=>1,'listorder'=>0,'level'=>3,'groupid'=>3);
        $data[]=array('name'=>'status','title'=>'状态','pid'=>90,'status'=>1,'listorder'=>0,'level'=>3,'groupid'=>3);
        $data[]=array('name'=>'listorder','title'=>'排序','pid'=>90,'status'=>1,'listorder'=>0,'level'=>3,'groupid'=>3);
        //$data[]=array('name'=>'deleteall','title'=>'批量删除','pid'=>90,'status'=>1,'listorder'=>0,'level'=>3,'groupid'=>3);
        //$data[]=array('name'=>'statusallok','title'=>'批量审核','pid'=>90,'status'=>1,'listorder'=>0,'level'=>3,'groupid'=>3);

        foreach($data as $sql){
            //$this->db->add($sql);
        }

        $str  = "<tr>
              <td><input name='listorders[\$id]' type='text' size='2' value='\$listorder'></td>
              <td>\$spacer\$title</td>
              <td>\$name</td>
              <td>\$status</td>
              <td>\$str_manage</td>
            </tr>";

        import('@.ORG.Tree');

        foreach($this->groups as $key=>$res){

            $result = $this->db->where("groupid={$res['id']}")->select();


            $array = array();
            if (empty($result)) {
                $result = array();
            }
            foreach ($result as $r) {
                $r['str_manage'] = '<a href="'.U('Node/add',array( 'pid' => $r['id'],'groupid'=>$r['groupid'])).'">添加</a> |
                <a href="'.U('Node/edit',array( 'id' => $r['id'])).'">编辑</a> |
                <a href="javascript:confirm_delete(\''.U('Node/delete',array( 'id' => $r['id'])).'\',\''.L('confirm',array('message'=>$r['cname'])).'\')">删除</a> ';
                $r['parentid'] = $r['pid'];
                $r['status']==1 ? $r['status']= '启用' : $r['status']='禁止';
                $array[] = $r;
            }

            $tree = new Tree($array);
            $tree->icon = array('│ &nbsp;&nbsp;&nbsp;', '├─ ', '└─');
            $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
            $data = $tree->get_tree(1, $str);

            $nodes[$res['id']]['data']  = $data;
            $nodes[$res['id']]['groupinfo'] = $res;
        }

        $this->assign('nodes', $nodes);
        $this->display();
    }


    function edit()
    {

        $id =  intval($_GET['id']);;
        $vo = $this->db->getById($id);
        $this->assign('groupid', $vo['groupid']);
        $pid =  intval($vo['pid']);

        import('@.ORG.Tree' );
        $result = $this->db->select();
        foreach($result as $r) {
            if($r['status']!=1) continue;
            $r['selected'] = $r['id'] == $pid ? 'selected' : '';
            $r['parentid']=$r['pid'];
            $array[] = $r;
        }
        $str  = "<option value='\$id' \$selected>\$spacer \$title</option>";
        $tree = new Tree ($array);
        $nodes = $tree->get_tree(0, $str,$pid);
        $this->assign('nodes', $nodes);
        $this->assign('udate', $vo );
        $this->display();

    }

    function add()
    {
        $groupid = intval($_GET['groupid']);
        $pid = intval($_GET['pid']);

        import('@.ORG.Tree');
        $result = $this->db->select();

        foreach($result as $r) {
            if($r['status']!=1 || $r['level']==3) continue;
            $r['selected'] = $r['id'] == $pid ? 'selected' : '';
            $r['parentid']=$r['pid'];
            $array[] = $r;
        }
        $str  = "<option value='\$id' \$selected>\$spacer \$title</option>";
        $tree = new Tree ($array);
        $nodes  = $tree->get_tree(0, $str,$pid);
        $this->assign('nodes', $nodes);
        $this->assign('groupid', $groupid);
        $this->display();
    }

    function update()
    {
        if($_POST['pid']){
            $level =$this->db->getById($_POST['pid']);
            $_POST['level']=$level['level']+1;
        }else{
            $_POST['level']=1;
        }

        $model = D('Node');

        if (false === $model->create()) {
            $this->error($model->getError ());
        }

        if (false !== $model->save()) {

            // savecache('Node');

            if($_POST['isajax'])
                $this->assign('dialog','1');

            $jumpUrl = $_POST['forward'] ? $_POST['forward'] : U('Node/index');

            $this->assign('jumpUrl', $jumpUrl);
            $this->success(L('edit_ok'));
        } else {
            $this->success (L('edit_error').': '.$model->getDbError());
        }
    }

    function insert() {

        if($_POST['pid']){
            $level = $this->db->getById($_POST['pid']);
            $_POST['level'] = $level['level']+1;
        }else{
            $_POST['level'] = 1;
        }
        $model = D('Node');

        if (false === $model->create ()) {
            $this->error( $model->getError());
        }

        $id = $model->add();

        if ($id !==false) {

            // savecache('Node');

            if($_POST['isajax'])
                $this->assign('dialog','1');
            $jumpUrl = $_POST['forward'] ? $_POST['forward'] : U(MODULE_NAME.'/index');

            $this->assign('jumpUrl',$jumpUrl);
            $this->success('添加成功!');
        } else {
            $this->error('添加失败: '.$model->getDbError());
        }
    }
}